<?php


require_once("../lib/fun.php");
require_once("../lib/pdo.php");
$path = str_replace('\\', '/', dirname(__FILE__)) . "/";
define('PHP_PAY',$path."payment/");
require_once  PHP_PAY.'vendor/autoload.php';

date_default_timezone_set('Asia/Shanghai');
$wxConfig = require_once PHP_PAY.'examples/wxconfig.php';

//微信支付参数
$wx_config = require("./config.php");

$wxConfig['app_id']  = $wx_config['app_id'];
$wxConfig['mch_id']  = $wx_config['mch_id'];
$wxConfig['md5_key']  = $wx_config['key'];


define('WX_CONFIG',$wx_config);


//vip数据
$db_vip = db('lu_vip');
$_vip = $db_vip->select();
$param = I("param.");
$act = I("param.act",'wx_back');
//小程序支付
if($param['act'] == 'app'){
    //获取用户d
    
    $rule  = [
        'id'  => [
            'type'         => 'number',
            'is_necessary' => 1,
            'name' => '订单ID',
        ],
        'act'  => [
            'type'         => 'string',
            'is_necessary' => 1,
            'name' => 'act',
        ],
         'token'  => [
            'type'         => 'string',
            'is_necessary' => 1,
            'name' => 'token',
        ],
    ];
    $check = CheckParams($param,$rule);
    if(!$check['success']){
        $msg = explode(";",$check['msg']);
        json([
            'code' => 0,
            'msg'  => $msg[0] ?? "",
        ]);
    }

    $db_user = db('lu_user');
    $token = str_replace(" ","+",I("token"));
    $uid = authcode($token,'DECODE',"youluda",7*24*3600);
    $user_info = $db_user->where(['id' => $uid])->find();
    if(empty($user_info['openid'])){
         json([
            'code' => 0,
            'msg' => '获取openid失败',
        ]);
    }
    $db = db("lu_order");
    $vip_data = $_vip;
    $vip_data = returnArrByKey($_vip,'id');
    if(!isset($vip_data[$param['id']])){
        // json([
        //     'code' => 0,
        //     'msg'  => '参数错误',
        // ]);
    }
    $vip_data = $vip_data[$param['id']];
    $order_no =  makeOrderNum();
    $add_order = [
            'vip_id' => $param['id'],
            'order_sn' => $order_no,
            'price' => $vip_data['money'],
            'type' => 1,
            'create_time' => time(),
            'uid' => $user_info['id'],
            ];
    $id = $db->add($add_order);
    if($id){
        $tradeNo = time() . rand(1000, 9999);
        // 订单信息
        $payData = [
            'body'         => '微信支付',
            'subject'      => '微信支付',
            'trade_no'     => $order_no,
            'time_expire'  => time() + 600, // 表示必须 600s 内付款
            'amount'       => $vip_data['money'] * 100, // 微信沙箱模式，需要金额固定为3.01
            'return_param' => '',
            'client_ip'    => isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1', // 客户地址
        ];
        $client = new \Payment\Client(\Payment\Client::WECHAT, $wxConfig);
        $res    = $client->pay(\Payment\Client::WX_CHANNEL_APP, $payData);
        json([
                'code' => 1,
                'info' => $res,
            ]);
        
    }else{
        json([
            'code' => 0,
            'msg'  => '提交失败',
        ]);
    }
    
}


